//url 地址验证
function isExal(url) {
    var reg = /^(((ht|f)tps?):\/\/)?[\w-]+(\.[\w-]+)+([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?$/

    if (reg.test(url)) {
        return true;
    } else {
        return false;
    }
}

// 日期格式化处理
// function format() {
//     var date = new Date();
//     var res = "YYYY-MM-DD hh:mm:ss"
//     var obj = {
//         'Y+': date.getFullYear(),
//         'M+': date.getMonth(),
//         'D+': date.getDate(),
//         'h+': date.getHours(),
//         'm+': date.getMinutes(),
//         's+': date.getSeconds()
//     }
//     for (k in obj) {
//         obj[k] < 10 ? '0' + obj[k] : obj[k];
//         var reg = new RegExp(k);
//         console.log(reg);
//         res = res.replace(reg,obj[k]);
//         console.log(res);
//     }
//     console.log(obj);
//     return res;
// }
// format();


Date.prototype.pattern = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份         
        "d+": this.getDate(), //日         
        "h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, //小时         
        "H+": this.getHours(), //小时         
        "m+": this.getMinutes(), //分         
        "s+": this.getSeconds(), //秒         
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度         
        "S": this.getMilliseconds() //毫秒         
    };
    var week = {
        "0": "/u65e5",
        "1": "/u4e00",
        "2": "/u4e8c",
        "3": "/u4e09",
        "4": "/u56db",
        "5": "/u4e94",
        "6": "/u516d"
    };
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    if (/(E+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "/u661f/u671f" : "/u5468") : "") + week[this.getDay() + ""]);
    }
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        }
    }
    return fmt;
}

// 处理url
function getUrlParams(url) {
    var res = {}
    var params = url.slice(url.indexOf('?') + 1);
    params = params.split('&');
    for (var i = 0; i < params.length; i++) {
        var index = params[i].indexOf('=');
        var key = params[i].slice(0, index);
        var value = params[i].slice(index + 1);
        res[key] = value;
    }
    return res
}
//返回顶部 
function reTop() {
    var returnTop = document.querySelector('.returnTop');
    var timer = null;
    var isTop = true;
    //滚动条滚动时触发
    window.addEventListener("scroll", function () {
        //显示回到顶部按钮
        var box = document.documentElement;
        // 当滚动到底后 让 返回顶部的div显示出来
        if (Math.ceil(box.clientHeight + box.scrollTop) == box.scrollHeight) {
            returnTop.style.display = "block";
        }
        else {
            returnTop.style.display = "none";
        }
        //回到顶部过程中用户滚动滚动条，停止定时器
        if (!isTop) {
            clearInterval(timer);
        };
        isTop = false;
    })
    //动画返回顶部
    returnTop.onclick = function () {
        //设置定时器
        timer = setInterval(function () {
            //获取滚动条距离顶部高度
            var osTop = document.documentElement.scrollTop || document.body.scrollTop;
            var ispeed = Math.floor(-osTop / 7);
            document.documentElement.scrollTop = document.body.scrollTop = osTop + ispeed;
            //到达顶部，清除定时器
            if (osTop == 0) {
                clearInterval(timer);
            };
            isTop = true;
        }, 30);
    }

}

// 验证登录
function checkLogin() {
    var nologin = document.querySelector(".nologin");
    var login = document.querySelector(".login");
    var token = localStorage.getItem("token");
    if (token) {
        login.style.display = "block";
        nologin.style.display = "none";
    } else {
        login.style.display = "none";
        nologin.style.display = "block";
    }
    //退出登录
    var quit = document.querySelector("#quit");
    quit.onclick = function () {
        localStorage.clear();
    }
}

//
// 固定导航栏
function fixedD() {
    var head_dw = document.querySelector(".head_dw");
    var mar = document.querySelector(".head_dw .mar");
    window.addEventListener("scroll", function () {
        var h = head_dw.offsetTop;
        var scrollTop = document.documentElement.scrollTop;
        if (scrollTop > h) {
            head_dw.style.position = "fixed";
            head_dw.style.top = 0;
            head_dw.style.left = "50%";
            head_dw.style.marginLeft = -600 + "px";
            mar.style.display = "block";
        } else {
            head_dw.style.position = "relative";
            mar.style.display = "none";
        }
    })
}
//
// 搜索
function Search() {
    var input = document.querySelector("input");
    var search = document.querySelector("#search");
    search.addEventListener("click", function () {
        sessionStorage.setItem("key", input.value);
        location.href = "./search.html";
    })
}

//promise获取数据
function ajax(){
    
}

